package com.wva.video.controller;

import cn.hutool.core.util.StrUtil;
import com.wva.cache.exception.TryLockTimeoutException;
import com.wva.common.annotation.authority.Open;
import com.wva.common.domain.dto.PageQueryDTO;
import com.wva.common.domain.vo.PageQueryVO;
import com.wva.common.exception.BusinessException;
import com.wva.common.status.StatusEnum;
import com.wva.common.utils.R;
import com.wva.common.utils.ValidateUtils;
import com.wva.video.cache.VideoCache;
import com.wva.video.domain.dto.VideoSubmissionDTO;
import com.wva.video.domain.po.VideoCategory;
import com.wva.video.domain.vo.VideoItemDTO;
import com.wva.video.domain.vo.VideoVO;
import com.wva.video.service.VideoCategoryService;
import com.wva.video.service.VideoCoverService;
import com.wva.video.service.VideoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @Author: hong
 * @Date: 2025/2/16 00:00:24
 */
@Api(tags = "视频稿件相关接口")
@RequestMapping("/video")
@RestController
public class VideoController {
    @Autowired
    VideoService videoService;
    @Autowired
    VideoCategoryService videoCategoryService;
    @Autowired
    VideoCoverService videoCoverService;
    @Resource
    VideoCache videoCache;

    @ApiOperation("投稿")
    @PostMapping
    R<Object> submission(@Validated @RequestBody VideoSubmissionDTO videoSubmissionDTO) {
        //校验转载链接
        if (videoSubmissionDTO.getType() == 1) {
            ValidateUtils.validate(videoSubmissionDTO, VideoSubmissionDTO.Repost.class);
        }

        //校验标签
        for (String tag : videoSubmissionDTO.getTags()) {
            if (StrUtil.isEmpty(tag) || tag.length() > 10) {
                throw new BusinessException(StatusEnum.PARAMS_CHECK_FAIL, "标签长度只能是1~10个字符");
            }
        }

        //校验分类
        VideoCategory category = videoCategoryService.getById(videoSubmissionDTO.getCategoryId());
        if (category == null) {
            throw new BusinessException("分类不存在");
        }

        //新增稿件
        videoService.submission(videoSubmissionDTO);

        //校验分类
        return R.ok().message("投稿成功");
    }


    @ApiOperation("获取公开稿件")
    @Open
    @GetMapping("/{videoId}")
    R<VideoVO> getPublicWork(@PathVariable Long videoId) throws TryLockTimeoutException, InterruptedException {
        VideoVO videoVO = videoCache.read(videoId);
        // VideoWorkVO videoWorkVO = videoWorkService.getPublicWorkById(videoId);
        if (videoVO == null) throw new BusinessException("视频不存在");
        return R.ok(videoVO);
    }

    @ApiOperation("获取用户公开视频列表")
    @Open
    @GetMapping("/list/by-user/{userId}")
    R<PageQueryVO<VideoItemDTO>> listByUserId(@PathVariable Long userId, @Validated PageQueryDTO pageQueryDTO) {
        //查询数据库中用户收藏的视频
        if (StrUtil.isEmpty(pageQueryDTO.getSortBy())) pageQueryDTO.setSortBy("update_time");
        PageQueryVO<VideoItemDTO> videos = videoService.listUserPublishVideo(userId, pageQueryDTO);
        return R.ok(videos);
    }

}
